<?php


namespace app\api\controller\School;


use app\api\controller\BaseController;
use app\api\model\School\SchoolAreaModel;
use app\api\model\School\SchoolModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class School extends BaseController
{
    /**
     * @api {get} /get_school_area_more 学校地区列表
     * @apiGroup 学校
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getSchoolAreaMore(Request $request)
    {
        $list = SchoolAreaModel::getSchoolAreaWhereMore([['is_show','=',1]]);
        return Util::returnJson(self::CODE_SUCCESS, $list);
    }

    /**
     * @api {get} /get_school_list 学校列表
     * @apiGroup 学校
     * @apiParam {int} page 页码
     * @apiParam {int} limit 数据条数
     * @apiParam {int} area_id 区域ID
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getSchoolList(Request $request)
    {
        $param = $request->param();
        $where = [
            ['s.is_show','=',1]
        ];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $area_id = !empty($param['area_id']) ? $param['area_id'] : 0;
        if(!empty($area_id)){
            array_push($where,['s.area_id','=',$area_id]);
        }
        $list = SchoolModel::getSchoolListWhere($page,$limit,$where,"s.*,sa.area_name");
        $count = SchoolModel::getSchoolCountWhere($where);
        foreach ($list as &$item){
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
            $item['details'] = !empty($item['details']) ? htmlspecialchars_decode($item['details']) : "";

        }
        unset($item);

        return Util::returnPageJson(self::CODE_SUCCESS,$count,$list,"");
    }

    /**
     * @api {get} /get_school_info 学校详情
     * @apiGroup 学校
     * @apiParam {int} id 学校ID
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function getSchoolInfo(Request $request)
    {
        $param = $request->param();
        $school = SchoolModel::getSchoolWhere([['s.id','=',$param['id']]],"s.*,sa.area_name");
        $school['img'] = ImageUtil::i()->replaceUrl($school['img']);
        $images = !empty($school['slider_img']) ? json_decode($school['slider_img'],true) : [];
        foreach ($images as &$image){
            $image = ImageUtil::i()->replaceUrl($image);
        }
        $school['slider_img'] = $images;
        $school['details'] = !empty($school['details']) ? htmlspecialchars_decode($school['details']) : "";
        return Util::returnJson(self::CODE_SUCCESS, $school, "");
    }

}